import java.awt.Component;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Song {
	
	private String filePath;
	private String songTitle;
	
	// Mutator function for the file path
	public void setFilePath (String filePath) {
		this.filePath = filePath;
	}
	
	// Accessor function for the file path
	public String getFilePath () {
		return this.filePath;
	}
	
	// Mutator function for the song title
	public void setSongTitle (String songTitle) {
		this.songTitle = songTitle;
	}
	
	// Accessor function for the song title
	public String getSongTitle () {
		return this.songTitle;
	}
	
	/*
	 * This method removes the file extension from the song title and returns it.
	 * After this, when the program displays the song title, the extension will not show,
	 * making the program more visually appealing.
	 */
	
	public String removeFileExtension (String songTitle) {
		String separator = System.getProperty("file.separator");
		String fixedSongTitle;
		int lastSeparatorIndex = songTitle.lastIndexOf(separator);
		if (lastSeparatorIndex == -1) {
			fixedSongTitle = songTitle;
		}
		else {
			fixedSongTitle = songTitle.substring(lastSeparatorIndex + 1);
		}
		int extensionIndex = fixedSongTitle.lastIndexOf(".");
		if (extensionIndex == -1) {
			return fixedSongTitle;
		}
		return fixedSongTitle.substring(0, extensionIndex);
	}
	
	/*
	 * Uses JFileChooser to open up a prompt so the user can add a song to the playlist
	 */
	
	public Song createNewSong (Component parent) {
		JFileChooser findFile = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter ("MP3 files", "mp3");
		findFile.setFileFilter(filter);
		int returnVal = findFile.showOpenDialog(parent);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			this.setFilePath(findFile.getSelectedFile().getPath());
			this.setSongTitle(findFile.getSelectedFile().getName());
		}
		return this;
	}
}